<?php

namespace app\index\dao;

use app\common\model\Margin;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * MarginDao 数据访问对象
 * Class MarginDao
 * @package app\index\dao
 * @method Margin findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method Margin insertActive(array $params, array $allow_field = [])
 * @method Margin updateActive(Margin $model, array $params, array $allow_field = [])
 * @method Margin findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method Margin updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(Margin $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class MarginDao
{
    use DaoTrait;

    /**
     * @var Margin
     */
    private $baseModel;

    /**
     * MarginDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = Margin::class;
    }

    public function selectByMemberId($memberId, $page, $page_size)
    {

        return Margin::where('memberId', $memberId)
            ->with([
                'lawyer' => fn($query) => $query->field(['id', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl', 'type'])
                    ->append(['headImg', 'headImgThumb', 'cropImg', 'showName'])
                    ->hidden(['headImgUrl']),
                'trade' => fn($query) => $query->field(['id', 'orderNo']),
            ])
            ->order('id', 'DESC')
            ->paging($page, $page_size)
            ->select();
    }
}
